<?php
class order_detail_model extends MY_model
{
    public $id;
    public $order_id;
    public $product_id;
    public $quantity;
    public $sub_total;

    public function get_elements()
    {
        $query = $this->db->query("SELECT * FROM order_detail");
        return $this->to_domain_models($query->result());
    }

    public function get_element_by_id($id)
    {
        $query = $this->db->query("SELECT * FROM order_detail WHERE id = ?",array($id));
        return $this->to_domain_model($query->row());
    }

    public function get_elements_by_order_id($order_id)
    {
        $query = $this->db->query("SELECT * FROM order_detail WHERE order_id = ?",array($order_id));
        return $this->to_domain_models($query->result());
    }

    public function add_order_detail($data)
    {
        $this->db->insert('order_detail',$data);
        if($this->db->affected_rows() > 0)
        {
            return true;
        }
        return false;
    }

    public function delete_by_order_id($order_id)
    {
        $this->db->delete('order_detail',array('order_id' => $order_id));
        if($this->db->affected_rows() > 0)
        {
            return true;
        }
        return false;
    }

    // -------------------------------------------------------------------------------- PRIVATE PROPERTIES
    public function get_product_name()
    {
        return $this->products_model->get_element_by_id($this->product_id)->name;
    }

    public function get_product_price()
    {
        return $this->products_model->get_element_by_id($this->product_id)->price;
    }

    // -------------------------------------------------------------------------------- PRIVATE METHOD

    // Private method
    public function to_domain_model($item)
    {
        $model = new order_detail_model();
        $model->id = isset($item->id) ? $item->id : '' ;
        $model->order_id = isset($item->order_id) ? $item->order_id : '' ;
        $model->product_id = isset($item->product_id) ? $item->product_id : '' ;
        $model->quantity = isset($item->quantity) ? $item->quantity : '0' ;
        $model->sub_total = isset($item->sub_total) ? $item->sub_total : '0' ;
        return $model;
    }
}